package com.lfm.jdbc.preparedstatement_;

import java.io.FileInputStream;
import java.sql.*;
import java.util.Properties;
import java.util.Scanner;

@SuppressWarnings({"all"})
public class PreparedStatement_ {
    public static void main(String[] args) throws Exception{


        String admin_name = "";
        String admin_pwd = "";

        Scanner scanner = new Scanner(System.in);
        System.out.print("请输入管理员名字: ");
        admin_name = scanner.nextLine();
        System.out.print("请输入管理员密码: ");
        admin_pwd = scanner.nextLine();


        Properties properties = new Properties();
        properties.load(new FileInputStream("mariadb.properties"));

        String user = properties.getProperty("user");
        String password = properties.getProperty("password");
        String driver = properties.getProperty("driver");
        String url = properties.getProperty("url");

        //1. 加载驱动
        Class.forName(driver);

        //2. 获得连接
        Connection connection = DriverManager.getConnection(url, user, password);

        //Statement statement = connection.createStatement();

        //? 相当于占位符
        String sql = "select name, pwd from admin where name = ? and pwd = ?";

        //3. 获得PreparedStatement
        java.sql.PreparedStatement preparedStatement = connection.prepareStatement(sql);
        preparedStatement.setString(1, admin_name);//第n个占位符就填n， 是什么类型就是set某类型
        preparedStatement.setString(2, admin_pwd);


        // 这里能再加入sql，否则读到的是问号而不是占位的
        //ResultSet resultSet = preparedStatement.executeQuery(sql);

        ResultSet resultSet = preparedStatement.executeQuery();

        if (resultSet.next()) {
            System.out.println("成功");
        } else {
            System.out.println("失败");
        }

        resultSet.close();
        connection.close();
        preparedStatement.close();
    }
}
